import { defineStore } from 'pinia'
let sign = 'aiApp-'
const useLoginStore = defineStore('userInfo', {
	state: () => {
		return {
			name: "沣沣",
			count: 0,
			userInfo: uni.getStorageSync(`${sign}userInfo`) || null,//用户信息,
		}
	},
	getters: {
		doubleCount: (state) => state.count * 2, // ✨
	},
	// actions 用来修改 state
	actions: {
		/** @description 登录用户储存的数据 */
		setUserInfo(objInfo : any) {
			console.log('用户储存的数据 ==>', objInfo)
			// if (objInfo && objInfo.uid) {
			// 	let UserLoginInfo = { ...objInfo }
			// 	this.userInfo = UserLoginInfo;
			// 	this.username = UserLoginInfo.username || "";
			// 	uni.setStorageSync(`${sign}userInfo`, this.userInfo)
				
			// 	uni.$emit('refresh_orderReload') // 触发刷新全局事件
			// 	if ( getCurrentPages().length > 1) {
			// 		uni.navigateBack()
			// 	} else {
			// 		uni.switchTab({
			// 			url: '/pages/index/homepage',
			// 			success() {
			// 				// uni.$emit('isBinding') // 触发首页事件
			// 			}
			// 		});
			// 	}
			// }
		},
		/** @description 更新本地缓存的用户数据 */
		updateUserInfo(objInfo : any) {
			// console.log('更新本地缓存的用户数据',this.userInfo);
			let tempUsername = this.userInfo;
			let tempObj = {
				...tempUsername,
				...objInfo,
			}
			this.userInfo = tempObj
			uni.setStorageSync(`${sign}userInfo`, this.userInfo)
		},
		/** @description 更新本地缓存数据 */
		getUserStorage() {
			this.userInfo = uni.getStorageSync(`${sign}userInfo`) || null;
		},
		/** @description 登录过期 清空缓存 */
		setClear() {
			// console.log("======> 清空缓存");
			this.userInfo = null;
			uni.removeStorageSync(`${sign}userInfo`)
			/* uni.switchTab({
				url: '/pages/index/homepage'
			}); */
			// uni.reLaunch({
			// 	url: '/pages/index/login'
			// });
		},
		/** @name 退出登录 */
		logoutClick() {
			this.userInfo = null;
			uni.removeStorageSync(`${sign}userInfo`)
			// uni.switchTab({
			// 	url: '/pages/index/homepage'
			// });
			// uni.clearStorage()  // 清空本地缓存
			uni.showToast({
				title: '已退出登录',
				icon: 'none',
				mask: true,
				duration: 1000,
			});
			uni.switchTab({
				url: '/pages/index/homepage'
			});
		},
	}
})
export default useLoginStore